package com.yxt.service;

import java.util.ArrayList;
import java.util.List;

import com.yxt.dao.DBOperator;
import com.yxt.model.Valley;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ValleyService {
	private DBOperator dbHelper;

	public ValleyService(Context context) {
		dbHelper = new DBOperator(context);
	}
	
	public List<Valley> getValleyByCondtion(String condition) {
		List<Valley> list = new ArrayList<Valley>();
		SQLiteDatabase db = dbHelper.getDataBase();
		StringBuffer sql = new StringBuffer();
		sql.append("select code,name,type,parentcode from valley");
		if (!"".equals(condition)) {
			sql.append(" where ");
			sql.append(condition);
		}
		Cursor cs = db.rawQuery(sql.toString(),null);
		while (cs.moveToNext()) {
			int index = 0;
			Valley valley = new Valley();
			valley.setCode(cs.getString(index++));
			valley.setName(cs.getString(index++));
			valley.setType(cs.getString(index++));
			valley.setParentCode(cs.getString(index++));
			list.add(valley);
		}
		cs.close();
		return list;
	}
	

	public void closeDB() {
		dbHelper.close();
	}

}
